package com.hex.ds.hdtp.core.app.data.transfer.handler.impl;

import com.hex.ds.hdtp.core.app.common.util.JaveShellUtil;
import com.hex.ds.hdtp.core.app.data.contrast.service.IContrastTableStructureService;
import com.hex.ds.hdtp.core.app.data.transfer.converter.IDataTransferLogConverter;
import com.hex.ds.hdtp.core.app.data.transfer.converter.IDataTransferSqlStructureConverter;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferListService;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferLogService;
import com.hex.ds.hdtp.core.app.data.transfer.service.IDataTransferTaskService;
import com.hex.ds.hdtp.core.app.metadata.service.ISourceConfInfoService;
import com.hex.ds.hdtp.core.app.metadata.service.ISourceInfoService;
import com.hex.ds.hdtp.core.inf.metadata.repository.impl.SourceConfInfoRepository;
import lombok.extern.slf4j.Slf4j;
import org.postgresql.copy.CopyManager;
import org.postgresql.core.BaseConnection;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Map;

@Slf4j
@Service("ORACLE_GAUSS_FILE")
public class OracleGaussTransferHandlerFileService extends AbstractDataTransferHandlerFileService {

    public OracleGaussTransferHandlerFileService(ISourceInfoService sourceInfoService, ISourceConfInfoService sourceConfInfoService, IDataTransferLogService dataTransferLogService, IDataTransferListService dataTransferListService, IDataTransferTaskService dataTransferTaskService, IDataTransferLogConverter dataTransferLogConverter, IDataTransferSqlStructureConverter dataTransferSqlStructureConverter, IContrastTableStructureService contrastTableStructureService, JaveShellUtil javeShellUtil, SourceConfInfoRepository sourceConfInfoRepository) {
        super(sourceInfoService, sourceConfInfoService, dataTransferLogService, dataTransferListService, dataTransferTaskService, dataTransferLogConverter, dataTransferSqlStructureConverter, contrastTableStructureService, javeShellUtil, sourceConfInfoRepository);
    }

    /**
     * @Method copyFromFile
     * @Param
     * @Param baseConn
     * @Param tempFilePath
     * @Param targetTableName
     * @Param delimiter
     * @Param targetEncoding
     * @Param resultMap
     * @Return void
     * @Description 从指定的文件中读取数据，并将其导入到指定的表中
     * @Author Yin.Yang
     * @Date 2023/10/18 16:26
     * @Version V2.0
     */
    public void copyFromFile(BaseConnection baseConn, String tempFilePath, String targetTableName, String delimiter, String targetEncoding, Map resultMap) throws SQLException, IOException {
        CopyManager copyManager = new CopyManager(baseConn);
        FileInputStream fileInputStream = new FileInputStream(tempFilePath);
        String doTransferSql = "COPY " + targetTableName + " from STDIN DELIMITER " + "'" + delimiter + "'" + " ENCODING " + "'" + targetEncoding + "'";
        resultMap.put("doTransferSql", doTransferSql);
        copyManager.copyIn(doTransferSql, fileInputStream);
    }
}
